﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace SharePointUtilitiesForDevelopers.Mapper
{
    /// <summary>
    /// A mapper that maps the xml web service response to a <see cref="SharePointField"/>.
    /// </summary>
    public class SharePointFieldMapper: IMapper<XElement, SharePointField>
    {

        /// <summary>
        /// Maps the source type to the target type.
        /// </summary>
        /// <param name="from">The object to map from.</param>
        /// <returns>The mapped object.</returns>
        public SharePointField Map(XElement from)
        {
            return MapList(from).SingleOrDefault();
        }

        /// <summary>
        /// Maps the source type to a list of the target type.
        /// </summary>
        /// <param name="from">From.</param>
        /// <returns></returns>
        public IEnumerable<SharePointField> MapList(XElement from)
        {
            try
            {
                var fields = SharePointMapper.MapResponseToList<SharePointField>(
                    MapInternal,
                    element =>
                    (string.Compare(element.Name.LocalName, "Field", StringComparison.InvariantCultureIgnoreCase) == 0) &&
                    SharePointXml.GetAttributeAsString(element, "ID") != string.Empty,
                    from);

                return fields.OrderBy(field => field.DisplayName).ToList();
            }
            catch (Exception ex)
            {
                throw new SharePointMapperException<XElement, IEnumerable<SharePointField>>(ex);
            }
        }

        /// <summary>
        /// Maps the element to a domain object.
        /// </summary>
        /// <param name="element">The element to map from.</param>
        /// <returns>The mapped object.</returns>
        private static SharePointField MapInternal(XElement element)
        {
            return new SharePointField
                       {
                           Id = SharePointXml.GetAttributeAsString(element, "ID"),
                           FieldType = SharePointXml.GetAttributeAsEnum(element, "FieldType", SharePointFieldType.Invalid, true),
                           SourceId = SharePointXml.GetAttributeAsString(element, "SourceID"),
                           Name = SharePointXml.GetAttributeAsString(element, "Name"),
                           DatabaseColumnName = SharePointXml.GetAttributeAsString(element, "ColName"),
                           DisplayName =
                               string.IsNullOrEmpty(SharePointXml.GetAttributeAsString(element, "DisplayName"))
                                   ? SharePointXml.GetAttributeAsString(element, "Name")
                                   : SharePointXml.GetAttributeAsString(element, "DisplayName"),
                           StaticName = SharePointXml.GetAttributeAsString(element, "StaticName"),
                           Hidden = SharePointXml.GetAttributeAsBoolean(element, "Hidden"),
                           Sealed = SharePointXml.GetAttributeAsBoolean(element, "Sealed"),
                           Definition = element,
                           BuiltIn = SharePointBuiltInFieldId.Contains(SharePointXml.GetAttributeAsString(element, "ID")),
                           FromBaseType = SharePointXml.GetAttributeAsBoolean(element, "FromBaseType"),
                           List = SharePointXml.GetAttributeAsString(element, "List"),
                           ShowField = SharePointXml.GetAttributeAsString(element, "ShowField")
                       };
        }
    }
}
